<?php
	/**
	*用户管理类
	**/
	class RoleClass {
		// 构造函数
		public function __constract(){
		}

		// 角色组列表
		public function getList($data){
			global $db;

			// 获得页码条数
			$db->query("select 
							role.id,
							role.name,
							role.create_time,
							user.name as creator_name,
							role.description
						from admin_role as role
						left join admin_user as user
						on role.creator=user.id","");
			$roleList = $db->prepare->fetchAll();

			return $roleList;
		}

		// 添加角色组
		public function add($data){
			global $db, $session;
			$loginUserId = $session->get("userId");
			$name = $data->name;
			$description = $data->description;

			// 事务开始
			$db->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$db->PDO->beginTransaction();

			// 查询角色名成是否已经存在
			$db->query("select * 
						from admin_role
						where
							name=:name
						for update",
						array(
							"name"=>$name
							)
						);
			$result = $db->prepare->fetchAll();
			if($result){
				$db->PDO->rollBack();
				returnAjaxJson(400, "角色名称已经存在了，请重新命名！", null);
			}

			// 插入一条新数据
			$db->query("insert into admin_role(
							name,
							creator,
							description) 
						values(
							:name,
							:creator,
							:description
						)", 
						array(
							"name"=>$name,
							"creator"=>$loginUserId,
							"description"=>$description
							)
						);
			$result = $db->prepare->rowCount();
			if(!$result){
				$db->PDO->rollBack();
				returnAjaxJson(400, "操作失败！", null);
			}

			$db->PDO->commit();
			// 事物结束
		}

		// 编辑角色组
		public function edit($data){
			global $db;
			$id = $data->id;
			$name = $data->name;
			$description = $data->description;

			// 事务开始
			$db->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$db->PDO->beginTransaction();

			// 查询角色名成是否已经存在
			$db->query("select * 
						from admin_role
						where
							name=:name and
							id!=:id
						for update",
						array(
							"name"=>$name,
							"id"=>$id
							)
						);
			$result = $db->prepare->fetchAll();
			if($result){
				$db->PDO->rollBack();
				returnAjaxJson(400, "角色名称已经存在了，请重新命名！", null);
			}

			// 更新数据
			$db->query("update admin_role 
						set
							name=:name,
							description=:description
						where
							id=:id",
						array(
							"name"=>$name,
							"description"=>$description,
							"id"=>$id
							)
						);
			$result = $db->prepare->rowCount();
			if(!$result){
				$db->PDO->rollBack();
				returnAjaxJson(400, "操作失败！", null);
			}

			$db->PDO->commit();
			// 事物结束
		}

		// 删除角色组
		public function del($data){
			global $db;
			$id = $data->id;

			// 事务开始
			$db->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$db->PDO->beginTransaction();

			// 查询admin_user_role表中的数据
			$db->query("select * from admin_user_role 
						where 
							roleid=:id",
						array(
							"id"=>$id
							)
						);
			$result = $db->prepare->fetchAll();
			if($result){
				$db->PDO->rollBack();
				returnAjaxJson(400, "有账户绑定了这个角色组，不可以删除！", null);
			}

			// 删除admin_role_menu表中的数据
			$db->query("delete from admin_role_menu
						where 
							roleid=:id",
						array(
							"id"=>$id
							)
						);

			// 删除admin_role表中的数据
			$db->query("delete from admin_role
						where 
							id=:id",
						array(
							"id"=>$id
							)
						);

			$db->PDO->commit();
			// 事物结束
		}

		// 为角色组分配权限
		public function assignPermissions($data){
			global $db, $session;
			$loginUserId = $session->get("userId");
			$roleId = $data->roleId;
			$arrMenuId = $data->arrMenuId;

			// 事务开始
			$db->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$db->PDO->beginTransaction();

			// 锁定admin_role_menu表排它锁
			$db->PDO->exec("lock tables admin_role_menu write");

			// 删除admin_role_menu所有roleid为$roleid的数据
			$db->query("delete from admin_role_menu
						where 
							roleid=:roleId",
						array(
							"roleId"=>$roleId
							)
						);

			// admin_role_menu插入数据
			$arrLen = count($arrMenuId);
			for($i=0; $i<$arrLen; $i++){
				$menuId = $arrMenuId[$i];
				$db->query("insert into admin_role_menu(
								roleid,
								menuid,
								creator
							) values(
								:roleId,
								:menuId,
								:creator
							)",
							array(
								"roleId"=>$roleId,
								"menuId"=>$menuId,
								"creator"=>$loginUserId
								)
							);
				$result = $db->prepare->rowCount();
				if(!$result){
					$db->PDO->rollBack();
					returnAjaxJson(400, "操作失败！", null);
				}
			}
			// 释放所有表锁
			$db->PDO->exec('unlock tables');
			$db->PDO->commit();
			// 事物结束
		}

		// 查询角色组已经分配的权限
		public function getPermissions($data){
			global $db;
			$roleId = $data->roleId;

			// 获得页码条数
			$db->query("select 
							menu.id,
							menu.name
						from admin_role_menu as role_menu
						left join admin_menu as menu
						on role_menu.menuid=menu.id
						where
							role_menu.roleid=:roleId
						",array(
							"roleId"=>$roleId
							));
			$roleList = $db->prepare->fetchAll();

			return $roleList;
		}
	}
?>